package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.service.MemberService;
import com.itheima.service.OrderService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/orderlist")
public class OrderListController {
    @Reference
    private OrderService orderService;
    @Reference
    private MemberService memberService;

    /**
     * 预约列表分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult=orderService.pageQuery(queryPageBean);
            return new Result(true,MessageConstant.GET_ORDER_LIST_SUCCESS,pageResult);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ORDER_LIST_FAIL);
        }
    }

    /**
     * 添加新的手机预约
     * @param map
     * @param setmealId
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Map<String,String> map, String setmealId){
        try{
            map.put("setmealId",setmealId);
            map.put("orderType","电话预约");
            return orderService.addOrder(map);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"服务器繁忙");
        }
    }

    /**
     * 预约订单编辑数据回显
     * @param id
     * @return
     */
    @RequestMapping("/findByOrderId")
    public Result findByOrderId(int id){
        try {
            Order order=orderService.findByOrderId(id);
            return new Result(true,MessageConstant.GET_ORDER_LIST_SUCCESS,order);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ORDER_LIST_FAIL);
        }
    }

    /**
     * 修改预约信息
     * @param order
     * @return
     */
    @RequestMapping("/updateOrderAndMember")
    public Result updateOrderAndMember(@RequestBody Order order){
        try {
            Member member = order.getMember();
            orderService.edit(order);
            memberService.updateMember(member);
            return new Result(true,MessageConstant.EDIT_ORDER_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_ORDER_FAIL);
        }
    }


    /**
     * 更改预约状态
     * @param order
     * @return
     */
    @RequestMapping("/changeStatus")
    public Result changeStatus(@RequestBody Order order){
        try {
            orderService.changeStatus(order);
            return new Result(true,MessageConstant.MODIFY_ORDERSTATUS_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.MODIFY_ORDERSTATUS_FAIL);
        }
    }

    @RequestMapping("/delete")
    public Result delete(int id){
        try {
            orderService.delete(id);
            return new Result(true,MessageConstant.DELETE_ORDER_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(true,MessageConstant.DELETE_ORDER_FAIL);
        }

    }
}
